package com.lzx.iteam.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.lzx.iteam.R;

import java.util.ArrayList;

/**
 * Created by zhangshuai on 15/10/9.
 */
public class VoteAdapter extends BaseAdapter {

    private ArrayList<String> mVotes;
    private Context mContext;
    private OnUpdateLinster mUpdateLinster;

    public VoteAdapter(ArrayList<String> mVotes,Context mContext){
        this.mVotes = mVotes;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mVotes.size();
    }

    @Override
    public Object getItem(int i) {
        return mVotes.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null){
            holder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.vote_item_layout,null);
            holder.mImageView = (ImageView) view.findViewById(R.id.vote_item_iv);
            holder.mTitle = (TextView) view.findViewById(R.id.vote_item_tv_title);
            holder.mContent = (EditText) view.findViewById(R.id.vote_item_et_content);
            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }

        if ( i == (mVotes.size() -1)){
            holder.mImageView.setBackgroundResource(R.drawable.vote_add_seletor);
            holder.mImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mUpdateLinster.onUpdateAdd(i);
                }
            });
        }else{
            holder.mImageView.setBackgroundResource(R.drawable.vote_remove_seletor);
            holder.mImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mUpdateLinster.onUpdateRemove(i);
                }
            });

        }

        holder.mTitle.setText("选项" + (char)(i + 65) + ":");
        holder.mContent.setText(mVotes.get(i));

        return view;
    }

    class ViewHolder{
        private ImageView mImageView;
        private TextView mTitle;
        private EditText mContent;
    }

    public interface OnUpdateLinster{
        void onUpdateAdd(int i);
        void onUpdateRemove(int i);
    }

    public void setOnUpdateLinster(OnUpdateLinster linster){
        this.mUpdateLinster = linster;
    }
}
